package com.holyn.guofang.viewcommon.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.holyn.guofang.R;
import com.holyn.guofang.model.webhttp.wo.UpdateInfoWo;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Holyn on 2017/6/8.
 */

public class UpdateAppDialog2 extends Dialog {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_version)
    TextView tvVersion;
    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.tv_sure)
    TextView tvSure;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;

    private String downUrl;

    private OnClickListener onSureClickListener = null;
    private OnClickListener onCancelClickListener = null;

    public UpdateAppDialog2(@NonNull Context context) {
        super(context, com.holyn.mylibrary.R.style.custom_dialog);
        initView();
    }

    public UpdateAppDialog2(@NonNull Context context, OnClickListener onSureClickListener, OnClickListener onCancelClickListener) {
        super(context, com.holyn.mylibrary.R.style.custom_dialog);
        initView();
        this.onSureClickListener = onSureClickListener;
        this.onCancelClickListener = onCancelClickListener;
    }

    private void initView() {
        View dialogView = LayoutInflater.from(getContext()).inflate(R.layout.dialog_update_dialog2, null);
        setContentView(dialogView);
        ButterKnife.bind(this);

        setCanceledOnTouchOutside(false);
    }

    public OnClickListener getOnSureClickListener() {
        return onSureClickListener;
    }

    public void setOnSureClickListener(OnClickListener onSureClickListener) {
        this.onSureClickListener = onSureClickListener;
    }

    public OnClickListener getOnCancelClickListener() {
        return onCancelClickListener;
    }

    public void setOnCancelClickListener(OnClickListener onCancelClickListener) {
        this.onCancelClickListener = onCancelClickListener;
    }

    @OnClick({R.id.tv_sure, R.id.tv_cancel})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_sure:
                Intent intent = new Intent();
                intent.setData(Uri.parse(getDownUrl()));//Url 就是你要打开的网址
                intent.setAction(Intent.ACTION_VIEW);
                UpdateAppDialog2.this.getContext().startActivity(intent); //启动浏览器
                if (onSureClickListener != null){
                    onSureClickListener.onClick(UpdateAppDialog2.this, 0);
                } else {
                    UpdateAppDialog2.this.dismiss();
                }
                break;
            case R.id.tv_cancel:
                if (onCancelClickListener != null){
                    onCancelClickListener.onClick(UpdateAppDialog2.this, 1);
                } else {
                    UpdateAppDialog2.this.dismiss();
                }
                break;
        }
    }

    public TextView getTvTitle() {
        return tvTitle;
    }

    public void setTvTitle(String text) {
        this.tvTitle.setText(text);
    }

    public TextView getTvVersion() {
        return tvVersion;
    }

    public void setTvVersion(String text) {
        this.tvVersion.setText("最新版本："+text);
    }

    public TextView getTvContent() {
        return tvContent;
    }

    public void setTvContent(String text) {
        this.tvContent.setText("更新内容："+text);
    }

    public String getDownUrl() {
        return downUrl;
    }

    public void setDownUrl(String downUrl) {
        this.downUrl = downUrl;
    }
}
